Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
console.log of backgroundColor shows "" instead of the color value

I have an button element with id "btn". I want to console.log the background color property it shows blank instead. What is the problem? Thank you.

let btnColor = document.getElementById("btn").style.backgroundColor;
console.log(btnColor)
#btn{
  display: block;
  margin: auto;
  width: 100px;
  height: 100px;
  border-radius: 3px;
  background-color: #74992e;
}
<button id="btn">Click on me</button>

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Use this:

let btnColor = window.getComputedStyle(document.getElementById(“btn”)).backgroundColor
console.log(btnColor)

it’s working fine for me

explain: sometimes window.getComputedStyle will get the style object of an elem so that if you call .backgroundColor then it’s working fine

summary two functions :)

  • window.getComputedStyle(element).backgroundColor

  • element.style.backgroundColor

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda